/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-03-31 14:58:28
 * @LastEditors: cuidm
 * @LastEditTime: 2022-09-26 09:58:17
 */
import React, { useState, useEffect } from "react";
import { Modal, Form, Button, Input,  message, Space, Tooltip } from "antd";
import { QuestionCircleFilled } from "@ant-design/icons";
import cmdbres from "@/data/cmdbres";
import request from "@/utils/request";
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const user = hcpUtils.getUser();
const { commonNameRegexp, commonCodeRegexp } = regexpUtil;
const CreateGroup = ({info, title, groupId, parentId, visible, setVisible, onCreateFinish})  => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);

	const onFinish = async (values) => {
		setLoading(true);
		let param =  {
			namespace: "default",
			author: user.staffName,
			...values
		}
		let url = cmdbres.createModelGroup;
		if(groupId) {
			param['id'] = groupId;
			url = cmdbres.updateModelGroup;
		}
		if(parentId) param['parentId'] = parentId;
		let res = await request({
			...url,
			data: param
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success("保存成功");
			if (typeof onCreateFinish == "function") onCreateFinish(res);
		} else {
			if(res) message.error(res.msg);
		}
	};

	const onCancel = () => {
		setVisible(false);
	}

	return (
		<Modal
			title={title}
			className="hcp-modal-sm"
			visible={visible}
			footer={null}
			onCancel={onCancel}
			destroyOnClose
			maskClosable={false}
			afterClose={()=>{form.resetFields()}}
		>
			<Form
				form={form}
				labelCol={{ span: 5 }}
				wrapperCol={{ span: 18 }}
				name="basic"
				onFinish={onFinish}
			>
				<Form.Item label="分类名称" required>
					<Form.Item 
						name="moduleClassificationName" 
						noStyle
						initialValue={groupId && info?.moduleClassificationName}
						rules={[
							commonNameRegexp(1,100, "分类名称")
						]}
					>
						<Input placeholder="请输入名称" maxLength={100} style={{width: '90%'}}/>
					</Form.Item>
					<Tooltip
						placement="right"
						title="分类名称不能为空,只允许包含1-100位中文、英文、数字及常用字符"
					>
						<QuestionCircleFilled style={{ fontSize: '16px', lineHeight: '32px', marginLeft: '10px' }}/>
					</Tooltip>
				</Form.Item>
				<Form.Item label="唯一标识" required >
					<Form.Item
						name="moduleClassificationCode"
						noStyle
						rules={[
							commonCodeRegexp(1,100)
						]}
						initialValue={groupId && info?.moduleClassificationCode}
						>
						<Input placeholder="请输入唯一标识" maxLength={100}  disabled={groupId} style={{width: '90%'}}/>
					</Form.Item>
					<Tooltip
						placement="right"
						title="唯一标识不能为空,只允许包含1-100位英文、数字、下划线、短横线和点"
					>
						<QuestionCircleFilled style={{ fontSize: '16px', lineHeight: '32px', marginLeft: '10px' }}/>
					</Tooltip>
				</Form.Item>
				<div className="hcp-modal-body-footer">
					<Form.Item wrapperCol={{ span: 24 }} >
						<Button onClick={onCancel}>取消</Button>
						<Button type="primary" htmlType="submit" loading={loading}>确认</Button>
					</Form.Item>
				</div>
			</Form>
		</Modal>
	)
}

export default CreateGroup;
